define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'subject/teacher/teacher/index' + location.search,
                    add_url: 'subject/teacher/teacher/add',
                    randadd_url: 'subject/teacher/teacher/randadd',
                    edit_url: 'subject/teacher/teacher/edit',
                    del_url: 'subject/teacher/teacher/del',
                    multi_url: 'subject/teacher/teacher/multi',
                    import_url: 'subject/teacher/teacher/import',
                    follow_url: 'subject/teacher/teacher/follow',
                    subject_url: 'subject/subject/index',
                    table: 'subject_teacher',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'avatar', title: __('Avatar'), operate: 'LIKE', events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'content', title: __('Content'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'job', title: __('Job'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table, events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'follow', 
                                    title: '关注列表', 
                                    icon: 'fa fa-users', 
                                    classname: 'btn btn-xs btn-primary btn-dialog', 
                                    extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'',
                                    url: $.fn.bootstrapTable.defaults.extend.follow_url,
                                },
                                {
                                    name: 'subject', 
                                    title: '课程列表', 
                                    icon: 'fa fa-align-justify', 
                                    classname: 'btn btn-xs btn-primary btn-dialog', 
                                    extend: 'data-toggle=\'tooltip\' data-area= \'["100%", "100%"]\'',
                                    url: $.fn.bootstrapTable.defaults.extend.subject_url,
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            $('.btn-randadd').click(function(){
                Fast.api.open($.fn.bootstrapTable.defaults.extend.randadd_url, '随机添加',{'area':['260px','260px']})
            })
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'subject/teacher/teacher/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'subject/teacher/teacher/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'subject/teacher/teacher/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        follow: function () 
        {
            var ids = Fast.api.query('ids')

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: `subject/teacher/teacher/follow?ids=${ids}`,
                    table: 'subject_teacher',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'createtime',
                columns: [
                    [
                        {field: 'createtime', title: __('Followtime'), sortable: true, operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        { field: 'business.nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'business.mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'business.gender', title: __('Gender'), searchList: {"0":__('Secrecy'),"1":__('Male'),"2":__('FeMale')}, formatter: Table.api.formatter.normal },
                        { field: 'business.deal', title: __('Deal'), searchList: {"0":__('Deal 0'),"1":__('Deal 1')}, formatter: Table.api.formatter.normal },
                        { field: 'business.auth', title: __('Auth'), searchList: {"0":__('Auth 0'),"1":__('Auth 1')}, formatter: Table.api.formatter.normal },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        randadd: function(){
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
